package com.qcs.ando;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class BaseActivity extends Activity {
    protected String TAG;

    protected static final String ON = ".ON";
    protected static final String OFF = ".OFF";
    protected static final String CALL = ".CALL";

    private ControlerActivityLifeBroadcast broadcast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TAG = this.getClass().getName();

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        broadcast = new ControlerActivityLifeBroadcast();
        broadcast.register();

        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    protected void execute(Bundle bund) {

    }

    protected void call(String name) {
        Intent intent = new Intent();
        intent.setAction(name + CALL);

        sendBroadcast(intent);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(broadcast);
        super.onDestroy();
    }

    class ControlerActivityLifeBroadcast extends BroadcastReceiver {

        public void register() {
            IntentFilter filter = new IntentFilter();
            filter.addAction(TAG + ON);
            filter.addAction(TAG + OFF);
            filter.addAction(TAG + CALL);

            registerReceiver(this, filter);
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            String tag = intent.getAction();

            if ((TAG + ON).equals(tag)) {

            }

            else if ((TAG + OFF).equals(tag)) {
                finish();
            }

            else if ((TAG + CALL).equals(tag)) {
                execute(intent.getExtras());
            }
        }
    }
}
